n = 9

def genRotateNums(n):
    a = [[0 for i in range(n)] for j in range(n)]
    # print(a)

    num = 1
    a[0][0] = num
    row = 0
    col = 1

    flag = 1 # right

    while a[row][col] == 0:
        num += 1
        a[row][col] = num

        if flag == 1: #right 向右，列加1，检测右边是否到头和是否已经有数字。
            col += 1
            if col == n:
                col = n-1
                flag = 2 # down 向下，顺时针则第二步要向下
            elif a[row][col] != 0:
                col = col -1
                flag = 2 # down 向下，顺时针则第二步要向下

        if flag == 2: #向下
            row += 1
            if row == n:
                row = n-1
                flag = 3 #向上
            elif a[row][col] != 0:
                row = row -1
                flag = 3 #向上

        if flag == 3: #向左
            col = col -1
            if col == -1:
                col = 0
                flag = 4
            elif a[row][col] != 0:
                col += 1
                flag = 4

        if flag == 4: #向上
            row = row -1
            if row == -1:
                row = 0
                flag = 1
                # print("111==========")
            elif a[row][col] != 0:
                row += 1
                col += 1  #要向右移动一位进行检测
                flag = 1
                # print("111==========222")

    print("==============================================")
    for i in range(n):
        for j in range(n):
            print(str(a[i][j])+ "\t", end="")
        print("\n")
    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
